package oldFiles;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import niagara.Logik_Datenbank;


public class Logik_Server
{
  private final ServerSocket server;
  public Logik_Server( int port ) throws IOException
  {
    server = new ServerSocket( port );
  }
  private void startServing()
  {
    while ( true )
    {
      Socket client = null;
      try
      {
        client = server.accept();
        handleConnection ( client );
      }
      catch ( IOException e ) {
        e.printStackTrace();
      }
      finally {
        if ( client != null )
          try { client.close(); } catch ( IOException e ) { e.printStackTrace(); }
      }
    }
  }
  private void handleConnection( Socket client ) throws IOException
  {
    InputStream  in  = client.getInputStream();
    Logik_Datenbank.getInstanz();
    OutputStream out = client.getOutputStream();
    
    String query = "SELECT * FROM benutzer;";
	ResultSet rs = (ResultSet) Logik_Datenbank.empfangeDaten(query);

	ResultSetMetaData rsmd;
	try
	{
		rsmd = rs.getMetaData();
		
		int spalten = rsmd.getColumnCount();
		
		byte[] arrayOfBytes = null;
		while (rs.next())
		{
			int i = 1;
			while(i<spalten+1)
			{
				arrayOfBytes[i-1] = rs.getByte(i);
				System.out.println(rs.getString(i)+" ");
				i++;
			}
		}
		for(Byte e: arrayOfBytes)
		{
			System.out.println(e);
		}
	rs.close();
	}
	catch (SQLException e)
	{
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }
  
  public static void main( String[] args ) throws IOException
  {
    Logik_Server server = new Logik_Server( 3141 );
    server.startServing();
  }
}